package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.product.pojo.dto.BrandAddNewDTO;
import cn.tedu.csmall.product.pojo.dto.BrandUpdateDTO;
import cn.tedu.csmall.product.pojo.dto.CategoryUpdateDTO;
import cn.tedu.csmall.product.pojo.vo.AlbumListItemVO;
import cn.tedu.csmall.product.pojo.vo.AlbumStandardVO;
import cn.tedu.csmall.product.pojo.vo.BrandListItemVO;
import cn.tedu.csmall.product.pojo.vo.BrandStandardVO;
import cn.tedu.csmall.product.service.IBrandService;
import cn.tedu.csmall.product.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 处理品牌相关请求的控制器
 *
 * @author 何惠民
 * @version 0.0.1
 */
@Validated
@Api(tags = "02品牌管理模块")
@Slf4j
@RestController
@RequestMapping("/brands")
public class BrandController {

    @Autowired
    private IBrandService brandService;

    public BrandController() {
        log.debug("创建控制器对象:BrandController");
    }

    @ApiOperation("添加品牌")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    public String  addNewBrand(BrandAddNewDTO brandAddNewDTO){
        log.debug("开始处理[添加品牌]的请求,参数{}",brandAddNewDTO);
        brandService.addNew(brandAddNewDTO);
        log.debug("添加测试数据成功");
        return "添加品牌成功";
    }
    @ApiOperation("根据id删除品牌")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "品牌id", required = true ,dataType = "long")
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult deleteById(@Range(min = 1, message = "删除品牌失败,删除的品牌ID无效")
                                 @PathVariable Long id){
        log.debug("开始处理[删除品牌]的请求,参数:{}",id);
        brandService.deleteById(id);
        log.debug("删除Id为{}的品牌成功",id);
        return JsonResult.ok();
    }

    @ApiOperation("修改品牌")
    @ApiOperationSupport(order = 300)
    @PostMapping("/update")
    public JsonResult<Void>  update(BrandUpdateDTO brandUpdateDTO){
        log.debug("开始处理[修改品牌]的请求,参数{}",brandUpdateDTO);
        brandService.update(brandUpdateDTO);
        log.debug("修改测试数据成功");
        return JsonResult.ok();
    }

    @ApiOperation("启用品牌")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParam(name = "id", value = "品牌id", required = true ,dataType = "long")
    @PostMapping("/{id:[0-9]+}/enable")
    public JsonResult setEnable(@Range(min = 1, message = "启用品牌失败,启用的品牌ID无效")
                                @PathVariable Long id){
        log.debug("开始处理[启用品牌]的请求,参数id={}",id);
        brandService.setEnable(id);
        return JsonResult.ok();
    }
    @ApiOperation("禁用品牌")
    @ApiOperationSupport(order = 320)
    @ApiImplicitParam(name = "id", value = "品牌id", required = true ,dataType = "long")
    @PostMapping("/{id:[0-9]+}/disable")
    public JsonResult setDisable(@Range(min = 1, message = "禁用品牌失败,禁用的品牌ID无效")
                                 @PathVariable Long id){
        log.debug("开始处理[禁用品牌]的请求,参数id={}",id);
        brandService.setDisable(id);
        return JsonResult.ok();
    }

    @ApiOperation("根据id查询品牌详情")
    @ApiOperationSupport(order = 400)
    @ApiImplicitParam(name = "id", value = "品牌id", required = true, dataType = "long")
    @GetMapping("/{id:[0-9]+}")
    public JsonResult<BrandStandardVO> getStandardById(@PathVariable Long id) {
        log.debug("开始处理[根据id查询品牌]的请求,参数{}",id);
        BrandStandardVO brandStandardVO = brandService.getStandardById(id);
        log.debug("查询品牌成功查询到{}",brandStandardVO);
        return JsonResult.ok(brandStandardVO);
    }


    @ApiOperation("查询品牌列表")
    @ApiOperationSupport(order = 420)
    @GetMapping("")
    public JsonResult<List<BrandListItemVO>> list(){
        log.debug("开始处理[查询品牌列表]的请求,参数无");
        List<BrandListItemVO> list = brandService.list();
        log.debug("查询品牌列表成功");
        return JsonResult.ok(list);
    }

    @ApiOperation("重建品牌缓存")
    @ApiOperationSupport(order = 600)
    @PostMapping("/cache/rebuild")
    public JsonResult<Void> rebuildCache(){
        log.debug("开始处理[重建缓存]的请求,参数无");
        brandService.rebuildCache();
        return JsonResult.ok();
    }
}
